package com.jcake.vimlike.internal;

import java.util.ArrayList;
import java.util.List;

import com.jcake.vimlike.api.ICommand;
import com.jcake.vimlike.api.ICommand.Status;

public class CommandManager {

	private static CommandManager commangeManager = new CommandManager();

	private List<ICommand> commands = new ArrayList<ICommand>();

	private CommandManager() {
	}

	public static CommandManager getInstance() {
		return commangeManager;
	}

	public void registerCommand(ICommand command) {
		this.commands.add(command);
	}

	public ICommand findCommand(String command) throws NoSuitableCommandException {
		int possibleMatches = 0;
		for (ICommand candidate : commands) {
			Status acceptStatus = candidate.accept(command);
			if (acceptStatus == ICommand.Status.YES) {
				return candidate;
			} else if (acceptStatus == ICommand.Status.MAYBE) {
				possibleMatches++;
			}
		}
		if (possibleMatches == 0) {
			throw new NoSuitableCommandException(command); 
		}
		return null;
	}
}
